home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / bor_ti.exe / TI1152.ASC < prev    next >
Text File  |  1992-11-04  |  2KB  |  133 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                           NUMBER  :  1152
  9.   VERSION  :  3.1
  10.        OS  :  ALL
  11.      DATE  :  November 4, 1992                         PAGE  :  1/2
  12.  
  13.     TITLE  :  Example of function returning pointer to function.
  14.  
  15.  
  16.  
  17.  
  18.  
  19.   This is an example demonstrating a function that returns a
  20.   pointer to a function.
  21.  
  22.   /*
  23.        This is an example demonstrating a function
  24.        that returns a pointer to a function.
  25.  
  26.        Expected output should be
  27.  
  28.        in bar()
  29.        A comes before B
  30.   */
  31.  
  32.   #include <stdio.h>
  33.  
  34.   /*
  35.        funcptr is a pointer to a function that
  36.        takes a single argument of type char and returns
  37.        nothing.
  38.   */
  39.  
  40.   typedef void (*funcptr)( char );
  41.  
  42.   /*
  43.        foo is a function takes a single argument of type char
  44.        and returns nothing.
  45.   */
  46.  
  47.   void foo( char letter )
  48.   {
  49.        printf( "%c comes before %c\n", letter, letter + 1 );
  50.   }
  51.  
  52.   /*
  53.        bar is a function that takes no arguments and returns
  54.        a pointer to a function that takes a single argument
  55.        of type char and returns nothing.
  56.   */
  57.  
  58.   funcptr bar( void )
  59.   {
  60.        printf( "in bar()\n" );
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                           NUMBER  :  1152
  75.   VERSION  :  3.1
  76.        OS  :  ALL
  77.      DATE  :  November 4, 1992                         PAGE  :  2/2
  78.  
  79.     TITLE  :  Example of function returning pointer to function.
  80.  
  81.  
  82.  
  83.  
  84.        return foo;
  85.   }
  86.  
  87.   void main( void )
  88.   {
  89.   /*
  90.        Line B below is equivalent to:
  91.             void (*f)(char) = bar();
  92.       Thus, f is a pointer to a function X that takes one argument
  93.       of type char and X returns a pointer to a function of type
  94.       void (*)(int).  Note that f doesn't point to the function
  95.       bar(); it points to what bar() returns.
  96.   */
  97.       /*  Line A is equivalent to Line B */
  98.  
  99.       /*  void (*f)(char) = bar(); */    /* Line A */
  100.       funcptr f = bar();                 /* Line B */
  101.   /*
  102.       Now call the function that f points to.  Since f points to
  103.       function foo(), which takes a single argument of type char,
  104.       we want to pass it a single char argument.
  105.   */
  106.       f('A');
  107.   }
  108.  
  109.  
  110.  
  111.   DISCLAIMER: You have the right to use this technical information
  112.   subject to the terms of the No-Nonsense License Statement that
  113.   you received with the Borland product to which this information
  114.   pertains.
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.